﻿using AspNetTraining.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspNetTraining
{
    public partial class GroupUsers : System.Web.UI.Page
    {
        public UserAccount SessionLogin
        {
            get
            {
                return (UserAccount)Session["LoggedUser"];
            }
            set
            {
                Session["LoggedUser"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (SessionLogin == null)
            {
                Response.Redirect("~/Account/Login.aspx?returnUrl=" + Request.Url.PathAndQuery);
            }

            GroupUserManager groupuser = new GroupUserManager();
            gvGroupUsers.DataSource = groupuser.GetGroupUser();
            gvGroupUsers.DataBind();

            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    //kiem tra co tai khoan nao trong nhom ko . neu ko co thi dc xoa
                    UserAccountManager user = new UserAccountManager();
                    if (user.CheckUserGroup(Request["id"]))
                    {
                        groupuser.DeleteGroupAccount(new System.Guid(Request["id"]));
                        Response.Redirect("~/GroupUsers.aspx");
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "alert(\"Tồn tại tài khoản trong nhóm quyền.Xóa không thành công!\");", true);
                    }
                    
                }
            }          
        }
        protected String GetUserNameByID(object ID)
        {
            if (ID == null)
                return null;
            UserAccountManager user = new UserAccountManager();
            return user.GetUserNameByUserID(new Guid(ID.ToString()));
        }
         protected void Unnamed_Click(object sender, EventArgs e)
         {
             foreach (GridViewRow row in gvGroupUsers.Rows)
             {
                 CheckBox chk = (CheckBox)row.FindControl("chkDeletes");
                 if (chk.Checked)
                 {
                     UserAccountManager user = new UserAccountManager();
                     Label lbl = (Label)row.FindControl("lblID");
                     user.DeleteAccountByUserName(lbl.Text);
                 }
             }
            Response.Redirect("~/GroupUsers.aspx");
        }

    }
}